package com.comm;

import android.app.Application;

import com.comm.ui.main.MainController;
import com.comm.module.ApiService;
import com.comm.module.ApiServiceImpl;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 实体对象的类加载器
 * Created by cxlin007 on 2017/10/5.
 */

public class BeanManager {

    private Application imContext;
    private static BeanManager beanManager;
    private static HashMap<Class, Object> beanMap = new HashMap<>();

    private BeanManager(Application application) {
        this.imContext = application;
    }

    synchronized public static BeanManager newInstance(Application application) {
        if (beanManager == null) {
            beanManager = new BeanManager(application);
        }
        return beanManager;
    }

    private <T> T getBean(Class<T> clazz) {
        if (clazz.equals(OkHttpClient.class)) {
            T t = (T) beanMap.get(clazz);
            if (t == null) {
                t = (T) createOkHttpClient();
                beanMap.put(clazz, t);
            }
            return t;
        } else if (clazz.equals(Retrofit.class)) {
            T t = (T) beanMap.get(clazz);
            if (t == null) {
                t = (T) createRetrofit();
                beanMap.put(clazz, t);
            }
            return t;
        } else if (clazz.equals(ApiService.class)) {
            return getRetrofit().create(clazz);

        } else if (clazz.equals(ApiServiceImpl.class)) {
            T t = (T) beanMap.get(clazz);
            if (t == null) {
                t = (T) new ApiServiceImpl(getBean(ApiService.class));
                beanMap.put(clazz, t);
            }
            return t;
        }
        return null;
    }

    private OkHttpClient createOkHttpClient() {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor())
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        return client;
    }

    private Retrofit createRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.vancoolver.ca/2.0.1/")
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit;
    }

    /**
     * 获取OKhttpClient对象
     *
     * @return
     */
    public OkHttpClient getOkHttpClient() {
        return getBean(OkHttpClient.class);
    }

    public Retrofit getRetrofit() {
        return getBean(Retrofit.class);
    }

    public ApiService getApiService() {
        return getBean(ApiService.class);
    }

    public ApiServiceImpl getApiServiceImpl() {
        return getBean(ApiServiceImpl.class);
    }
}
